package com.shelpe.services.sinbad.controller.sinbad;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.sinbad.controller.dto.EffectDto;
import com.shelpe.services.sinbad.service.rpt.AdgroupService;
import com.shelpe.services.sinbad.service.rpt.CampaignService;
import com.shelpe.services.sinbad.service.rpt.CreativeService;
import com.shelpe.services.sinbad.service.rpt.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/sinbadservice/v2")
@Api("v2版报表相关的API")
@Slf4j
public class ReportController {
	@Autowired
	private UserService userService;
	@Autowired
	private CampaignService campaignService;
	@Autowired
	private AdgroupService adgroupService;
	@Autowired
	private CreativeService creativeService;

	@ApiOperation(value = "用户效果数据摘要")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "all,pc, mobile,pcin,pcout,mobilein,mobileout,instation,outstation", paramType = "query", dataType = "string") })
	@RequestMapping(path = "/users/{user_id}/reports/@metric", method = RequestMethod.GET)
	public EffectDto userMetric(@PathVariable("user_id") long userID,
			@RequestParam(required = false, defaultValue = "all") String device,
			@RequestParam(name = "days", defaultValue = "0") int days,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("summariesMetricWithPage RequestParam userID:{} device:{} startDate:{} endDate:{}", userID, device,
				startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.userService.summariesMetric(userID, device, startDate, endDate);
	}

	@ApiOperation(value = "用户效果数据趋势")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "all,pc, mobile", paramType = "query", dataType = "string") })
	@RequestMapping(path = "/users/{user_id}/reports/@trend", method = RequestMethod.GET)
	public List<EffectDto> userTrend(@PathVariable("user_id") long userID,
			@RequestParam(name = "device", required = false, defaultValue = "all") String device,
			@RequestParam(name = "days", defaultValue = "0") int days,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("userEffectMetricPage RequestParam userID:{} device:{} startDate:{} endDate:{}", userID, device,
				startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.userService.userEffectTrend(userID, device, startDate, endDate);
	}

	@ApiOperation(value = "推广计划效果数据趋势")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "pc, mobile, all", paramType = "query", dataType = "string") })
	@RequestMapping(path = "/campaigns/{campaign_id}/reports/@trend", method = RequestMethod.GET)
	public List<EffectDto> campaignTrend(@PathVariable("campaign_id") long campaignID,
			@RequestParam(required = false, defaultValue = "0") int days,
			@RequestParam(name = "device", defaultValue = "all", required = false) String device,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("reportsTrend RequestParam campaignID:{} days:{} device:{} startDate:{} endDate:{}", campaignID, days,
				device, startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.campaignService.campaignTrend(campaignID, startDate, endDate, device);
	}

	@ApiOperation(value = "推广计划效果数据摘要")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "pc, mobile, all", paramType = "query", dataType = "string") })
	@RequestMapping(path = "/campaigns/{campaign_id}/reports/@metric", method = RequestMethod.GET)
	public EffectDto campaignMetric(@PathVariable("campaign_id") long campaignID,
			@RequestParam(required = false, defaultValue = "0") int days,
			@RequestParam(name = "device", defaultValue = "all", required = false) String device,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("summariesMetric RequestParam campaignID:{} days:{} device:{} startDate:{} endDate:{}", campaignID,
				days, device, startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.campaignService.campaignMetric(campaignID, startDate, endDate, device);
	}

	@ApiOperation(value = "推广组的效果数据趋势")
	@RequestMapping(method = RequestMethod.GET, path = "/adgroups/{adgroup_id}/reports/@trend")
	public List<EffectDto> adgroupTrend(@PathVariable("adgroup_id") long adgroupID,
			@RequestParam(required = false, defaultValue = "0") int days,
			@RequestParam(name = "device", defaultValue = "all", required = false) String device,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("reportsTrend RequestParam adgroupID:{} days:{} device:{} startDate:{} endDate:{}", adgroupID, days,
				device, startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.adgroupService.adgroupTrend(adgroupID, device, startDate, endDate);
	}

	@ApiOperation(value = "推广组效果数据摘要")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "pc, mobile, all", paramType = "query", dataType = "string") })
	@RequestMapping(method = RequestMethod.GET, path = "/adgroups/{adgroup_id}/reports/@metric")
	public EffectDto adgroupMetric(@PathVariable("adgroup_id") long adgroupID,
			@RequestParam(required = false, defaultValue = "0") int days,
			@RequestParam(name = "device", defaultValue = "all", required = false) String device,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("summariesMetric RequestParam adgroupID:{} days:{} device:{} startDate:{} endDate:{}", adgroupID, days,
				device, startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.adgroupService.adgroupMetric(adgroupID, device, startDate, endDate);
	}

	@ApiOperation(value = "创意效果数据趋势")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "pc, mobile, all", paramType = "query", dataType = "string") })
	@RequestMapping(method = RequestMethod.GET, path = "/creatives/{creative_id}/reports/@trend")
	public List<EffectDto> creativeTrend(@PathVariable("creative_id") long creativeID,
			@RequestParam(required = false, defaultValue = "0") int days,
			@RequestParam(name = "device", defaultValue = "all", required = false) String device,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("reportsTrend RequestParam adgroupID:{} days:{} device:{} startDate:{} endDate:{}", creativeID, days,
				device, startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.creativeService.creativeTrend(creativeID, device, startDate, endDate);
	}

	@ApiOperation(value = "创意效果数据摘要")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "pc, mobile, all", paramType = "query", dataType = "string") })
	@RequestMapping(method = RequestMethod.GET, path = "/creatives/{creative_id}/reports/@metric")
	public EffectDto creativeMetric(@PathVariable("creative_id") long creativeID,
			@RequestParam(required = false, defaultValue = "0") int days,
			@RequestParam(name = "device", defaultValue = "all", required = false) String device,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		log.info("summariesMetric RequestParam adgroupID:{} days:{} device:{} startDate:{} endDate:{}", creativeID,
				days, device, startDate, endDate);
		Date yesterday = DateUtils.addDays(new Date(), -1);
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		if (days > 0) {
			startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days + 1), "yyyy-MM-dd");
			endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
		}
		return this.creativeService.creativeMetric(creativeID, device, startDate, endDate);
	}

	/************ 辅助类方法 ***************/
	// private void from(String startDate, String endDate, int days) {
	// Date yesterday = DateUtils.addDays(new Date(), -1);
	// if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
	// startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -6),
	// "yyyy-MM-dd");
	// }
	// if (days > 0) {
	// startDate = DateFormatUtils.format(DateUtils.addDays(yesterday, -days +
	// 1), "yyyy-MM-dd");
	// }
	// endDate = DateFormatUtils.format(yesterday, "yyyy-MM-dd");
	// }

	/********** 混合型效果数据(趋势+摘要) ***************/
	@ApiOperation(value = "混合型效果数据(趋势+摘要)")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "device", defaultValue = "all", allowableValues = "pc, mobile, all", paramType = "query", dataType = "string") })
	@RequestMapping(method = RequestMethod.GET, path = "/users/{user_id}/@rpt")
	public EffectBacthDto metricWithTrend(@PathVariable("user_id") long userID,
			@RequestParam(name = "campaign_id", required = false, defaultValue = "0") long campaignID,
			@RequestParam(name = "adgroup_id", required = false, defaultValue = "0") long adgroupID,
			@RequestParam(required = false, defaultValue = "0") int days,
			@RequestParam(name = "device", required = false, defaultValue = "all") String device,
			@RequestParam(name = "start", required = false, defaultValue = "") String startDate,
			@RequestParam(name = "end", required = false, defaultValue = "") String endDate) {
		EffectBacthDto dto = new EffectBacthDto();
		if (campaignID > 0) {
			dto.setSummary(this.campaignMetric(campaignID, days, device, startDate, endDate));
			dto.setSeries(this.campaignTrend(campaignID, days, device, startDate, endDate));
		}
		if (adgroupID > 0) {
			dto.setSummary(this.adgroupMetric(adgroupID, days, device, startDate, endDate));
			dto.setSeries(this.adgroupTrend(adgroupID, days, device, startDate, endDate));
		}

		if (campaignID == 0 && adgroupID == 0) {
			dto.setSummary(this.userMetric(userID, device, days, startDate, endDate));
			dto.setSeries(this.userTrend(userID, device, days, startDate, endDate));
		}
		return dto;
	}

	@Data
	public static class EffectBacthDto {
		private EffectDto summary;
		private List<EffectDto> series;
	}
}
